<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/my_log_register.css">
</head>
<body >
    <div id="container" class="shadow p-3 mb-5 bg-white rounded">
        <div id="title">
            <h1 style="text-align: center; padding-top: 1rem;">注册</h1>
        </div>

        <form>
            <!-- 文本框 -->
            <div id="body_middle">
                <div class="row">
                  <div class="col">
                      <center>
                          <input type="text" class="form-control" id="username" name="username" placeholder="用户名/手机号/邮箱" style="width: 50%; text-align: center;">
                      </center>
                  </div>
                </div>
                <div class="row">
                    <div class="col">
                        <center>
                            <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" style="width: 50%; text-align: center">
                        </center>
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <center>
                            <input type="password" class="form-control" id="confirmpassword" name="confirmpassword" placeholder="请确认密码" style="width: 50%; text-align: center">
                        </center>
                    </div>
                </div>
            </div>
            <br>
                <!-- 按钮组件 -->
                <div style="text-align: center;">
                    <button type="button" id="submit" class="btn btn-outline-success" style="width: 6rem; margin-right: 2rem; ">提交</button> 
                    <button type="button" id="reset" class="btn btn-outline-success"  style="width: 6rem;" name="reset" >重置</button> 
                </div>
            </div>
        </form>
    </div>
    
    <script src="../static/js/jquery-3.5.1.min.js"></script>
    <script src="../static/js/bootstrap.min.js"></script>
    <script src="../static/js/register_btn.js"></script>
    <script src="../static/js/register_input_focus.js"></script>

    <script>
        // 点击注册时 执行的方法（获取数据，进行初步验证，验证通过则通过ajax向后台提交数据；否则提示验证不通过的信息）
                function jump() {
                    let username = $('#username').val();
                    let password = $('#password').val();
                    let confirmpassword = $('#confirmpassword').val();
                    if (username.length > 0 && password.length > 0 && password === confirmpassword) {
                        // ajax请求
                        $.ajax({
                            url: "/registerDone",
                            type: "POST",
                            data: $('#registerForm').serialize(),
                            dataType: "json",
                            success: function (res) {
                                console.log(res);
                                if(res.code===200){
                                    window.location.href="/login";
                                }else{
                                    alert_fn(res.msg);
                                }
                                
                            },
                            error: function (msg) {
                                alert_fn('网络或者服务器有问题');
                            }
                        })
                    } else {// 验证不通过，提示相关信息，并且阻止提交
                        alert_fn();
                        return false;
                    }
                }
        
                // 这个方法包装了alert的相关行为和显示的内容
                function alert_fn(msg) {
                    msg=msg || '密码不能为空，两次密码应该一致';
                    // 替换提示的内容
                    $('#msg').text(msg);
                    // 移除不显示的class，则alert将显示
                    $('.invisible').removeClass('invisible');
                    // 三秒中后，给alert增加不显示的class，则alert重新隐藏
                    setTimeout(() => {
                        $('.alert').addClass('invisible');
                    }, 3000);
                }
    </script>

</body>
</html>